//
//  CLReadCatalogView.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/16.
//

#import "CLReadCatalogView.h"
#import "CLReadCatalogCell.h"
#import "CLReadModel.h"
@implementation CLReadCatalogView

- (void)setReadModel:(CLReadModel *)readModel {
    _readModel = readModel;
    
    [self.tableView reloadData];
    [self scrollRecord];
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubviews];
    }
    return self;
}

- (void)addSubviews {
    self.tableView = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self addSubview:self.tableView];
}

- (void)scrollRecord {
    if (self.readModel) {
        [self.tableView reloadData];
        if (self.readModel.chapterListModels && !self.readModel.chapterListModels.isEmpty) {
            __block NSInteger row = -1;
            [self.readModel.chapterListModels enumerateObjectsUsingBlock:^(CLReadChapterListModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                if ([obj.chapterid isEqualToString:self.readModel.recordModel.chapterModel.chapterid]) {
                    row = idx;
                    *stop = YES;
                }
            }];
            if (row != -1) {
                WEAK_SELF;
                dispatch_async(dispatch_get_main_queue(), ^{
                    [weakSelf.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:row inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:NO];
                });
            }
        }
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = self.bounds;
}

// MARK: UITableViewDelegate,UITableViewDataSource


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.readModel) { return self.readModel.chapterListModels.count; }
    return 0;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    CLReadCatalogCell *cell = [CLReadCatalogCell cell:tableView];
    CLReadChapterModel *chapterListModel = self.readModel.chapterListModels[indexPath.row];
    cell.chapterName.text = chapterListModel.title;
    // 日夜间
    if ([CLUserDefaults boolForKey:CL_READ_KEY_MODE_DAY_NIGHT]) {

        cell.spaceLine.backgroundColor = [CL_COLOR_230_230_230 colorWithAlphaComponent:0.1];

    }else{ cell.spaceLine.backgroundColor = CL_COLOR_230_230_230; }

    // 阅读记录
    if ([self.readModel.recordModel.chapterModel.chapterid isEqualToString:chapterListModel.chapterid]) {
        cell.chapterName.textColor = CL_READ_COLOR_MAIN;
    }else{ cell.chapterName.textColor = CL_COLOR_145_145_145; }
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return CL_SIZE(CL_SPACE_50);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(catalogViewClickChapterWithCatalogView:chapterListModel:)]) {    
        [self.delegate catalogViewClickChapterWithCatalogView:self chapterListModel:self.readModel.chapterListModels[indexPath.row]];
    }
}

@end




